package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.engines.LoginEngine;
import com.example.myapplication.entity.User;
import com.example.myapplication.observers.LoginObserver;
import com.example.myapplication.room.entity.Student;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "sun";
    public static MainActivity instance;
    public static boolean firstOpen = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instance = this;

        View viewById = findViewById(R.id.image_corgi);
        if (firstOpen) viewById.setVisibility(View.GONE);


        Button myButtonTextView = findViewById(R.id.btn_show_textview);
        myButtonTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到TextView界面
                Intent intent = new Intent(MainActivity.this, TextViewActivity.class);
                startActivity(intent);
            }
        });

        Button myButton1 = findViewById(R.id.btn_bottom_1);
        myButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick");
            }
        });
        myButton1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.e(TAG, "OnLongClick");
                return false;
            }
        });
        myButton1.setOnTouchListener(new View.OnTouchListener() {
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e(TAG, "onTouch");
//                Log.e(TAG, "onTouch:" + event.getAction());
                return false;
            }
        });

        myButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到TextView界面
                Intent intent = new Intent(MainActivity.this, DialogPopActivity.class);
                startActivity(intent);
            }
        });

        ProgressBar progressBar = findViewById(R.id.progress_bar);
        progressBar.setMin(0);
        progressBar.setMax(100);
        progressBar.setProgress(50);

    }

    public void btnClick2(View view) {
        Log.e(TAG, "方便呀");
        //跳转界面
        Intent intent = new Intent(MainActivity.this, RecyclerActivity.class);
        startActivity(intent);
    }


    public void btnLogin(View view) {
        EditText editText1 = findViewById(R.id.edit_1);
        String username = editText1.getText().toString();

        EditText editText2 = findViewById(R.id.edit_2);
        String phone = editText2.getText().toString();

        if (phone != null && !phone.equals("")) Toast.makeText(this, "不用填手机号嘛", Toast.LENGTH_SHORT).show();

        EditText editText3 = findViewById(R.id.edit_3);
        String password = editText3.getText().toString();

        LoginEngine.login(username, password)
                .subscribe(new LoginObserver() {
                    @Override
                    public void success(User user) {
                        Toast.makeText(MainActivity.this, user.toString(), Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent();

                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

                        intent.setClass(MainActivity.this, HomeActivity.class);

                        startActivity(intent);

                    }

                    @Override
                    public void error(String message) {
                        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                });

//        Log.e(TAG, "然而并没有什么卵用");

    }


    public void btnClick3(View view) {
        //跳转界面
        startActivity(new Intent(MainActivity.this, AnimationActivity.class));
    }

    public void btn_glide(View view) {
        startActivity(new Intent(this, PicturesActivity.class));
    }

    public void btn_request(View view) {
        startActivity(new Intent(this, RequestActivity.class));
    }

    public void btn_database(View view) {
        Intent intent = new Intent(this, DatabaseActivity.class);
        intent.putExtra("name", "sun");

        Bundle bundle = new Bundle();
        bundle.putInt("age", 1);
        bundle.putString("school", "HIT");
        intent.putExtras(bundle);

        Student sun = new Student("孙佳豪", 25);
        intent.putExtra("student", sun);

        startActivity(intent);
    }

    public void btn_media(View view) {
        startActivity(new Intent(this, MediaActivity.class));
    }
}